from concurrent.futures import ThreadPoolExecutor, as_completed
from queue import Queue
import time

# 使用 Queue 存储任务
task2_futures = Queue()

def task2(url):
    # 模拟任务执行
    time.sleep(0.1)
    return f"Processed {url}"

def task1(links):
    with ThreadPoolExecutor(max_workers=300) as executor:
        for link in links:
            for url in link.urls:
                # 提交任务到线程池，并将 Future 对象放入队列
                future = executor.submit(task2, url)
                task2_futures.put(future)

        # 等待所有任务完成
        while not task2_futures.empty():
            future = task2_futures.get()
            try:
                result = future.result()
                print(result)
            except Exception as e:
                print(f"Task failed: {e}")

# 示例调用
class Link:
    def __init__(self, name, urls):
        self.name = name
        self.urls = urls

links = [
    Link("group1", ["url1", "url2"]),
    Link("group2", ["url3", "url4"])
]

task1(links)
